from django.shortcuts import render
from common.models import Users
from datetime import datetime
import hashlib
from django.core.paginator import Paginator
from django.db.models import Q

# Create your views here.
def index(request,page):
    '''浏览会员列表'''
    userData=Users.objects.all()
    # 获取搜索条件
    kw=request.GET.get("kw")
    sex=request.GET.get("sex")
    if kw is None:
        kw=""
    if sex is None:
        sex="2"
    search={"kw":kw,"sex":sex}
    # 筛选会员列表
    if kw is not None and kw!="":
        userData=userData.filter((Q(username__icontains=kw) | Q(name__icontains=kw)))
    if sex=="1" or sex=="0":
        userData=userData.filter(sex=sex) 
    # 分页信息
    pagelist=[]#页码
    p = Paginator(userData,5)
    if page is None or page == '':
        page = 1
    page = int(page)
    curUsersData = p.page(page)
    pagelist = p.page_range
    context={"userlist":curUsersData,"pagelist":pagelist,"search":search}
    return render(request,'admin/users/index.html',context)

def add(request):
    '''加载添加页面'''
    return render(request,'admin/users/add.html')

def insert(request):
    '''执行添加'''
    try:
        uobj=Users()
        uobj.username=request.POST["username"]
        uobj.name = request.POST["name"]
        pwd = request.POST["password"] #密码
        repwd=request.POST["repassword"]#重复密码
        if pwd!=repwd:# 密码不一致
            context = {"info": "两次输入密码不一致！"}
        else:
            m=hashlib.md5()
            m.update(bytes(pwd,encoding='utf8'))
            uobj.password = m.hexdigest()
            uobj.sex = request.POST["sex"]
            uobj.address = request.POST["address"]
            uobj.code = request.POST["code"]
            uobj.phone = request.POST["phone"]
            uobj.email = request.POST["email"]
            uobj.state=1
            uobj.addtime=datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            uobj.save()
            context = {"info": "添加成功！"}
    except Exception as err:
        context={"info":"添加失败！"}
    return render(request,'admin/info.html',context)

def delete(request,uid):
    '''删除会员'''
    try:
        uobj=Users.objects.get(id=uid)
        uobj.delete()
        context = {"info": "删除成功！"}
    except Exception as err:
        context={"info":"删除失败！"}
    return render(request,'admin/info.html',context)

def edit(request,uid):
    '''加载编辑会员信息'''
    try:
        uobj=Users.objects.get(id=uid)
        context={'user':uobj}
        return render(request, 'admin/users/edit.html',context)
    except Exception as err:
        context={"info":"查询信息失败！"}
    return render(request,'admin/info.html',context)

def update(request,uid):
    '''执行会员编辑'''
    try:
        uobj=Users.objects.get(id=uid)
        uobj.name = request.POST["name"]
        uobj.sex = request.POST["sex"]
        uobj.address = request.POST["address"]
        uobj.code = request.POST["code"]
        uobj.phone = request.POST["phone"]
        uobj.email = request.POST["email"]
        uobj.state=request.POST["state"]
        uobj.save()
        context = {"info": "修改成功！"}
    except Exception as err:
        context={"info":"修改失败！"}
    return render(request,'admin/info.html',context)

# 重置密码
def pwd(request,uid):
    '''加载会员重置密码页面'''
    try:
        uobj=Users.objects.get(id=uid)
        context={'user':uobj}
        return render(request, 'admin/users/pwd.html',context)
    except Exception as err:
        context={"info":"查询信息失败！"}
    return render(request,'admin/info.html',context)
def pwdSave(request,uid):
    '''执行会员密码重置'''
    try:
        uobj=Users.objects.get(id=uid)
        pwd = request.POST["password"] #密码
        repwd=request.POST["repassword"]#重复密码
        if pwd!=repwd:# 密码不一致
            context = {"info": "两次输入密码不一致！"}
        else:
            m=hashlib.md5()
            m.update(bytes(pwd,encoding='utf8'))
            uobj.password = m.hexdigest()
            uobj.save()
            context = {"info": "密码重置成功！"}
    except Exception as err:
        context={"info":"密码重置失败！"}
    return render(request,'admin/info.html',context)

